package com.gizwits.opensource.appkit.feeddog;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.gizwits.opensource.appkit.CommonModule.GosBaseActivity;
import com.gizwits.opensource.appkit.R;
import com.hisilicion.histreaming.GizWifiDevice;
import com.hisilicion.histreaming.GizWifiSDK;

public class feeddogActivity extends GosBaseActivity {
    private GizWifiDevice device;
    private Button m_feeddogButton;
    private String feed_dog_states ="feed_dog_on";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feeddog);
        initDevice();
        initView();
    }
    private void initDevice() {
        Intent intent = getIntent();
        device = (GizWifiDevice) intent.getParcelableExtra("GizWifiDevice");
    }
    private String switchStates(){
        String states = "feed_dog_on";
        if(feed_dog_states.equals("feed_dog_on")){
            states = "feed_dog_off";
        } else if (feed_dog_states.equals("feed_dog_off")){
            states = "feed_dog_on";
        }
        return states;
    }
    private void setText(){
        if(feed_dog_states.equals("feed_dog_on")) {
            m_feeddogButton.setText(R.string.feed_dog_on);
        } else if (feed_dog_states.equals("feed_dog_off")){
            m_feeddogButton.setText(R.string.feed_dog_off);
        }
    }

    private void initView(){
        m_feeddogButton = (Button)findViewById(R.id.feed_dog_bt);
        m_feeddogButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void  onClick(View view){
                String states = switchStates();

                int code = GizWifiSDK.sharedInstance().post(device.m_index,"feed_dog",states);

                if(code == 0){
                    feed_dog_states = states;
                    setText();
                } else if(code == -2){
                    Toast.makeText(feeddogActivity.this,"post error", 200).show();
                }

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}